using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

namespace xasset.samples
{
    public class Download : MonoBehaviour
    {
        public Transform layout;
        public LoadingBar loadingBar;
        private DownloadRequest _request;
        private DownloadRequestBatch _batch;

        private void OnDestroy()
        {
            _batch?.Cancel();
        }

        public void Run()
        {
            StartCoroutine(Loading());
        }

        private IEnumerator Loading()
        {
            InputField[] ins = layout.transform.GetComponentsInChildren<InputField>();
            _batch = DownloadRequestBatch.Create();
            foreach (var inputField in ins)
            {
                string url = inputField.text;
                var content1 = DownloadContent.Get(url, Assets.GetDownloadDataPath(Path.GetFileName(url)), url, 1024 * 1024 * 122);
                _batch.AddContent(content1);
            }

            // Logger.D($"Download {url}");
            // var content = DownloadContent.Get(url, Assets.GetDownloadDataPath(Path.GetFileName(url)), url,
            //     1024 * 1024 * 122);
            // _request = Downloader.DownloadAsync(content);
            // loadingBar.SetVisible(true);
            // while (!_request.isDone)
            // {
            //     var downloadedBytes = Utility.FormatBytes(_request.downloadedBytes);
            //     var downloadSize = Utility.FormatBytes(_request.downloadSize);
            //     var bandwidth = Utility.FormatBytes(_request.bandwidth);
            //
            //     var time = (_request.downloadSize - _request.downloadedBytes) * 1f / _request.bandwidth;
            //     var msg = $"{Constants.Text.Loading}{downloadedBytes}/{downloadSize}, {bandwidth}/s - 剩余：{(int)time}s";
            //     loadingBar.SetProgress(msg, _request.progress);
            //     yield return new WaitForSeconds(1);
            // }

            _batch.SendRequest();
            loadingBar.SetVisible(true);
            while (!_batch.isDone)
            {
                var downloadedBytes = Utility.FormatBytes(_batch.downloadedBytes);
                var downloadSize = Utility.FormatBytes(_batch.downloadSize);
                var bandwidth = Utility.FormatBytes(_batch.bandwidth);

                var time = (_batch.downloadSize - _batch.downloadedBytes) * 1f / _batch.bandwidth;
                var msg = $"{Constants.Text.Loading}{downloadedBytes}/{downloadSize}, {bandwidth}/s - 剩余：{(int)time}s";
                loadingBar.SetProgress(msg, _batch.progress);
                yield return new WaitForSeconds(1);
            }

            loadingBar.SetVisible(false);
        }

        public void Retry()
        {
            _batch?.Retry();
        }

        public void Pause()
        {
            _batch?.Pause();
        }

        public void UnPause()
        {
            _batch?.UnPause();
        }

        public void Cancel()
        {
            _batch?.Cancel();
        }

        public void Clear()
        {
            _batch?.Clear();
        }
    }
}